package com.lzhpo.admin.controller.sys;

import com.google.common.base.Objects;
import com.lzhpo.admin.entity.ReturnData;
import com.lzhpo.admin.entity.User;
import com.lzhpo.admin.service.UserService;
import com.lzhpo.core.utils.JsonResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> Author：lzhpo </p>
 * <p> Title：</p>
 * <p> Description：</p>
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    UserService userService;


    @GetMapping("/list")
    @ResponseBody
    public ReturnData<User> getUserList(Integer offset,Integer pageNumber,User user){
        ReturnData<User> peopleData = new ReturnData<User>();
        List<User> userList=userService.findUserList();
        peopleData.setTotal(userList.size());
        //得到user数据对象
        peopleData.setRows(userList.stream().skip(offset)
                .limit(pageNumber).collect(Collectors.toList()));
        return peopleData;
    }

    @PostMapping("/delById")
    @ResponseBody
    public  JsonResp deleteUserById(Integer id){
        userService.deleteUserById(id);
        return   JsonResp.success();
    }

    @PostMapping("/resetPdById")
    @ResponseBody
    public  JsonResp resetPdById(Integer id){
        userService.resetPdById(id);
        return   JsonResp.success();
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public JsonResp dragonConditionChange(
            Integer id,
            String password_old,
            String password_new,
            String password_new_confirm,
            String user_name,
            String nick_name,
            String type
    ) {
        /**
         * 先检查旧密码是否正确
         * 如果正确。查看两次密码是否输入值
         */
        boolean result = false;
        int code=0;
        if ("1".equals(type)) {
            if (StringUtils.isNoneBlank(password_new, password_new_confirm,user_name,nick_name)
                    && Objects.equal(password_new, password_new_confirm)) {
                nick_name=StringUtils.defaultIfBlank(nick_name,"麒麟子");
                int rows = userService.insertNewUser(password_new, user_name,nick_name);
                if (rows==-2){
                    code=rows;
                }
                result = rows >= 1;
            } else {
                return JsonResp.fail("");
            }
        } else {
            if (StringUtils.isNoneBlank(password_new, password_new_confirm, password_old)
                    && Objects.equal(password_new, password_new_confirm)) {
                int rows = userService.updatePassWordById(id, password_old, password_new);
                result = rows >= 1;
            } else {
                return JsonResp.fail("");
            }
        }
        return result ? JsonResp.success("操作成功") : JsonResp.fail(code==0?"操作失败":"该用户名已存在");

    }

}
